<template>
    <div class="CompanyView">
        <div class="Company">
            <div class="header">
                <span class="cg">场馆维护 / </span>
                <span>公司信息</span>
            </div>
            <div class="main">
                <div class="com" v-for="item, index in CompanyInfo" :key="index">
                    <div class="title">{{ item.fullname }}</div>
                    <div class="tex">
                        <div class="name mar">简称：{{ item.shortname }}</div>
                        <div class="address mar">地址：{{ item.address }}</div>
                        <div class="tel mar">电话：{{ item.mobile }}</div>
                    </div>
                    <el-pagination @current-change="currentChange" background layout="prev, pager, next" :total="totals"
                        v-model:page-size="searchForm.size" />
                    <div class="pic">
                        <div class="zj">相关证件</div>
                        <div class="tou">
                            <div class="left">
                                <el-upload action="#" list-type="picture-card" :auto-upload="false" class="hh">
                                    <el-icon>
                                        <Plus />
                                    </el-icon>

                                    <template #file="{ file }">
                                        <div>
                                            <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
                                            <span class="el-upload-list__item-actions">
                                                <span class="el-upload-list__item-preview"
                                                    @click="handlePictureCardPreview(file)">
                                                    <el-icon><zoom-in /></el-icon>
                                                </span>
                                                <span v-if="!disabled" class="el-upload-list__item-delete"
                                                    @click="handleDownload(file)">
                                                    <el-icon>
                                                        <Download />
                                                    </el-icon>
                                                </span>
                                                <span v-if="!disabled" class="el-upload-list__item-delete"
                                                    @click="handleRemove(file)">
                                                    <el-icon>
                                                        <Delete />
                                                    </el-icon>
                                                </span>
                                            </span>
                                        </div>
                                    </template>
                                </el-upload>

                                <el-dialog v-model="dialogVisible">
                                    <img w-full :src="dialogImageUrl" alt="Preview Image" />
                                </el-dialog>
                            </div>
                        </div>
                    </div>
                    <div class="zh">
                        <div class="zh-title">账号管理</div>
                        <div class="total" v-for="(item, index) in paymentInfo" :key="index">
                            <div class="zh-main" v-if="item.type!=2">
                                <div class="first">
                                    <div>支付宝</div>
                                    <div>支付宝账号：{{ item.alipay }}</div>
                                    <div class="qi">
                                        <span>启用</span>
                                        <el-switch v-model="value1" />
                                        <span @click="toEdit(item)">修改</span>
                                    </div>
                                    <p>若关闭此账户，请使用其他账号</p>
                                </div>
                            </div>
                            <div class="zh-main" v-else>
                                <div class="first">
                                    <div>绑定银行卡：{{ item.name }}</div>
                                    <div>开户名称：张大白</div>
                                    <!-- <div>卡号：{{ item.bank_account }}</div> -->
                                    <div class="qi">
                                        <span>启用</span>
                                        <el-switch v-model="value2" />
                                        <span @click="toEdit1(item)">修改</span>
                                    </div>
                                    <p>若关闭此账户，请使用其他账号</p>
                                </div>
                            </div>
                            <div class="zh-main">
                                <div class="first">
                                    <div class="xz">新增账号</div>
                                    <div>
                                        <div>新增类型</div>
                                        <div>
                                            <span class="zf" @click="showAdd = true;addForm.type=1" v-if="item.type!=2">支付宝</span>
                                            <span class="yh" @click="showAdd1 = true;addForm.type=2" v-else>银行卡</span>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>
                        <el-pagination @current-change="currentChange2" background layout="prev, pager, next"
                            :total="totals2" v-model:page-size="searchForm2.size" />
                    </div>
                    <!-- 新增弹框开始 -->
                    <el-dialog v-model="showAdd" title="新增/修改支付宝信息">
                        <el-form :model="addForm" class="addForm" label-width="90">

                            <el-form-item label="支付宝账号" prop="alipay">
                                <el-input placeholder="账号" v-model="addForm.alipay" />
                            </el-form-item>
                            <div class="qiyong">启用 <el-switch v-model="value2" /></div>
                            <el-form-item>
                                <el-button type="primary" @click="updatezfb" v-if="showEdit">修改</el-button>
                                <el-button type="primary" @click="addzfb" v-else>保存</el-button>
                            </el-form-item>
                        </el-form>
                    </el-dialog>
                    <!-- 新增弹框结束 -->
                    <!-- 新增弹框开始 -->
                    <el-dialog v-model="showAdd1" title="新增/修改银行卡信息">
                        <el-form :model="addForm" class="addForm" label-width="90">
                            <el-form-item label="开户银行" prop="name">
                                <el-input placeholder="银行" v-model="addForm.name" />
                            </el-form-item>
                            <!-- <el-form-item label="账号" prop="bank_account">
                                <el-input placeholder="账号" v-model="addForm.bank_account" />
                            </el-form-item> -->
                            <el-form-item label="开户地址(选填)" prop="bank_address">
                                <el-input placeholder="地址" v-model="addForm.bank_address" />
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" @click="updatezfb" v-if="showEdit2">确定</el-button>
                                <el-button type="primary" @click="addzfb" v-else>新增</el-button>
                            </el-form-item>
                        </el-form>
                    </el-dialog>
                    <!-- 新增弹框结束 -->
                </div>

            </div>
        </div>
    </div>
</template>
<script setup>
import { ref } from 'vue';
import { GetCompany, GetPayment, AddZfb, UpdateZfb } from "../../../api/stadium";
import { Delete, Download, Plus, ZoomIn } from "@element-plus/icons-vue";
const disabled = ref(false);
const dialogImageUrl = ref("");
const dialogVisible = ref(false);
// 定义弹框是否显示
const showAdd = ref(false);
const showAdd1 = ref(false);

// 定义是否显示编辑
const showEdit = ref(false);
const showEdit2 = ref(false);

const value1 = ref(true)
const value2 = ref(true)

const handleRemove = (file) => {
    console.log(file);
};

const handlePictureCardPreview = (file) => {
    dialogImageUrl.value = file.url;
    dialogVisible.value = true;
};

const handleDownload = (file) => {
    console.log(file);
};
// 添加支付宝,银行
const addForm = ref({
    alipay: '',
    name: '',
    bank_deposit: '',
    bank_address: '',
    is_default: '',
    company_id: '',
    
});
const addzfb = () => {
    AddZfb(addForm.value).then((res) => {
        console.log(res);
        if (res.data.code == 0) {
            showAdd.value = false;
            showAdd1.value = false;
            getpayment();
            addForm.value = {
                alipay: '',
                name: '',
                bank_deposit: '',
                bank_address: '',
                is_default: '',
                company_id: '',
            }
        } else {
            alert(res.data.msg || "添加失败");
        }
    })
        .catch(err => {
            // err.response&&err.response.data&&err.response.data.msg)
            if (err.response?.data?.msg) {
                alert(err.response.data.msg)
            }
        })
}
// 定义去编辑
const toEdit = item => {
    // 显示弹框
    showAdd.value = true;
    // 显示编辑
    showEdit.value = true;
    // 定义弹框的内容是编辑的item解构
    addForm.value = { ...item };
}
const toEdit1 = item => {
    // 显示弹框
    showAdd1.value = true;
    // 显示编辑
    showEdit2.value = true;
    // 定义弹框的内容是编辑的item解构
    addForm.value = { ...item };
}
// 定义修改支付宝
const updatezfb = () => {
    // 把addForm传入修改
    UpdateZfb(addForm.value)
        .then(res => {
            if (res.data.code == 0) {
                // 修改完重新获取最新的支付宝信息
                getpayment();
                // showEdit为false
                showEdit.value = false;
                showEdit2.value = false;
                // 隐藏弹框
                showAdd.value = false;
                showAdd1.value = false;
                // 清空addForm的值
                addForm.value = {
                    alipay: '',
                    name: '',
                    bank_deposit: '',
                    bank_address: '',
                    is_default: '',
                    company_id: '',
                }
            } else {
                alert("编辑失败")
            }
        })
        .catch(err => {
            console.log(err);
            alert("失败");
        })

}

// 银行支付宝信息
const paymentInfo = ref([]);
// 银行支付宝
const searchForm2 = ref({
    size: 1,
    company_id: '',
    bank_address: '',
    bank_deposit: '',
    name: '',
    alipay: '',
    type: '',
});
const getpayment = () => {
    GetPayment(searchForm2.value).then(res => {
        console.log(res.data.data);
        paymentInfo.value = res.data.data;
    })
}
getpayment();

const totals = ref(200)
const totals2 = ref(20)
// 公司信息
const CompanyInfo = ref([]);
// 搜索的条件 searchForm
const searchForm = ref({
    size: 1,
    shortname: '',
    phone: '',
    id: '',
    mobile: '',
    fullname: '',
    address: '',

});
const getCom = () => {
    GetCompany(searchForm.value).then(res => {
        console.log(res.data.data);
        CompanyInfo.value = res.data.data;
    })
}
getCom();

// 分页
const currentChange = (val) => {
    searchForm.value.current = val;
    getCom();
}

const currentChange2 = (val) => {
    searchForm2.value.current = val;
    getpayment();
}
</script>

<style>
.el-dialog__body {
    >img {
        width: 100%;
    }
}
</style>
<style lang="scss" scoped>
.CompanyView {
    background-color: #f5f5f5;
    height: 140vh;

    .Company {
        width: 80%;
        margin: 0 auto;

        .header {
            height: 54px;
            color: #5e5e5e;
            line-height: 60px;

            .cg {
                color: #282828;
                font-size: 18px;
            }
        }

        .main {
            background-color: #fff;
            overflow: hidden;

            .com {
                padding: 0 15px;
                height: 100vh;

                .title {
                    width: 300px;
                    line-height: 30px;
                    font-size: 18px;
                    color: white;
                    background-color: #a8bed0;
                    // padding: 0 15px;
                    text-align: center;
                    border-radius: 7px;
                    margin: 10px 0;
                }

                .tex {
                    // padding: 0 15px;

                    .mar {
                        margin: 10px 0;
                        color: rgb(58, 54, 54);
                    }

                }

                .pic {

                    .zj {
                        background-color: #a8bed0;
                        width: 80px;
                        text-align: center;
                        border-radius: 4px;
                        height: 30px;
                        line-height: 30px;
                        color: white;
                    }

                }

                .zh {

                    // overflow: auto;
                    // height: 200px;
                    .zh-title {
                        margin: 10px 0;
                        width: 80px;
                        background-color: #a8bed0;
                        text-align: center;
                        border-radius: 4px;
                        color: white;
                        height: 30px;
                        line-height: 30px;

                    }

                    .total {
                        display: flex;
                        justify-content: space-between;

                        .zh-main {
                            width: 248px;
                            padding: 24px;
                            border: 1px solid rgb(224, 214, 214);
                            color: #494444;
                            display: flex;

                            .first {
                                >div {
                                    margin-bottom: 10px;
                                }

                                .qi {
                                    span {
                                        display: inline-block;
                                        width: 40px;
                                        margin-right: 10px;
                                    }

                                    >span:nth-of-type(2) {
                                        margin-left: 30px;
                                        background-color: #a8bed0;
                                        display: inline-block;
                                        height: 30px;
                                        width: 60px;
                                        border-radius: 4px;
                                        cursor: pointer;
                                        line-height: 30px;
                                        text-align: center;
                                        color: white;
                                    }
                                }

                                p {
                                    font-size: 14px;
                                }

                                .zf,
                                .yh {
                                    display: inline-block;
                                    width: 60px;
                                    height: 38px;
                                    line-height: 38px;
                                    background-color: #a8bed0;
                                    text-align: center;
                                    border-radius: 4px;
                                    color: white;
                                    margin-top: 15px;
                                    margin-right: 20px;
                                    cursor: pointer;
                                }

                                .xz {
                                    font-size: 22px;
                                }
                            }
                        }
                    }


                }

                .qiyong {
                    margin-left: 10px;
                }
            }

        }
    }

}
</style>